import {wxUtils} from "./wx/wxUtils";
import {gameUtils, sdkUtils} from "./utils";

cc.Class({
  extends: cc.Component,

  properties: {
    display: cc.Sprite,

    restartBtn: cc.Button,
    startBtn: cc.Button,
    shareBtn: cc.Button,
    phbBtn: cc.Button,
    backBtn: cc.Button,

  },

  // LIFE-CYCLE CALLBACKS:

  onLoad() {

    const size = cc.view.getVisibleSize();
    wxUtils.getSharedCanvas().width = size.width;
    wxUtils.getSharedCanvas().height = size.height;
  },

  start() {
    this.tex = new cc.Texture2D();

    this.backBtn.node.active = false;
  },

  restartClick() {
    gameUtils.isRestart = true;
    wxUtils.postMessage({
      message: 'backGame',
    });
    cc.director.loadScene("GameScene");
  },

  beginClick() {
    wxUtils.postMessage({
      message: 'backGame',
    });
    cc.director.loadScene("GameScene");
  },

  phbClick() {
    wxUtils.postMessage({
      message: 'phb',
    });
    this.restartBtn.node.active = false;
    this.startBtn.node.active = false;
    this.shareBtn.node.active = false;
    this.phbBtn.node.active = false;
    this.backBtn.node.active = true;
  },

  backClick() {
    if (window.Game.toPhbType === "js") {
      wxUtils.postMessage({
        message: 'back',
      });

      this.restartBtn.node.active = true;
      this.startBtn.node.active = true;
      this.shareBtn.node.active = true;
      this.phbBtn.node.active = true;
      this.backBtn.node.active = false;

    } else if (window.Game.toPhbType === "start") {
      wxUtils.postMessage({
        message: 'backGame',
      })
      cc.director.loadScene("StartScene", null);
    } else {
      wxUtils.postMessage({
        message: 'back',
      })
      this.restartBtn.node.active = true;
      this.startBtn.node.active = true;
      this.shareBtn.node.active = true;
      this.phbBtn.node.active = true;
      this.backBtn.node.active = false;
    }
  },


  shareClick() {
    sdkUtils.onShare();
  },

  _updaetSubDomainCanvas() {
    if (!this.tex) {
      return;
    }
    this.tex.initWithElement(wxUtils.getSharedCanvas());
    this.tex.handleLoadedTexture();
    this.display.spriteFrame = new cc.SpriteFrame(this.tex);

  },

  update(dt) {
    this._updaetSubDomainCanvas();
  },
});
